using System;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using Microsoft.SharePoint.Administration;
using System.Collections.ObjectModel;

namespace UpdateMultipleListItemsSPDActivity.Features.UpdateMultipleListItemsSPDActivity
{
    /// <summary>
    /// This class handles events raised during feature activation, deactivation, installation, uninstallation, and upgrade.
    /// </summary>
    /// <remarks>
    /// The GUID attached to this class may be used during packaging and should not be modified.
    /// </remarks>

    [Guid("b25bbc2c-08f7-41b7-8544-d6b5effe1013")]
    public class UpdateMultipleListItemsSPDActivityEventReceiver : SPFeatureReceiver
    {
        // Uncomment the method below to handle the event raised after a feature has been activated.

        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            SPWebService myService = SPWebService.ContentService;
            SPWebConfigModification myModification = new SPWebConfigModification();
            myModification.Path = "configuration/System.Workflow.ComponentModel.WorkflowCompiler/authorizedTypes";
            myModification.Name = "authorizedType[@Assembly='UpdateMultipleListItemsSPDActivity, Version=1.0.0.0, Culture=neutral, PublicKeyToken=02cef9de13dc0f11'][@Namespace='UpdateMultipleListItemsSPDActivity'][@TypeName='*'][@Authorized='True']";
            myModification.Sequence = 0;
            myModification.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode;
            myModification.Value = "<authorizedType Assembly='UpdateMultipleListItemsSPDActivity, Version=1.0.0.0, Culture=neutral, PublicKeyToken=02cef9de13dc0f11' Namespace='UpdateMultipleListItemsSPDActivity' TypeName='*' Authorized='True' />";
            myService.WebConfigModifications.Add(myModification);
            myService.Update();
            myService.ApplyWebConfigModifications();
        }

        // Uncomment the method below to handle the event raised before a feature is deactivated.

        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            SPWebConfigModification configModFound = null;
            SPWebService myService = SPWebService.ContentService;
            Collection<SPWebConfigModification> modsCollection = myService.WebConfigModifications;

            // Find the most recent modification of a specified owner
            int modsCount1 = modsCollection.Count;
            for (int i = modsCount1 - 1; i > -1; i--)
            {
                if (modsCollection[i].Name == "authorizedType[@Assembly='UpdateMultipleListItemsSPDActivity, Version=1.0.0.0, Culture=neutral, PublicKeyToken=02cef9de13dc0f11'][@Namespace='UpdateMultipleListItemsSPDActivity'][@TypeName='*'][@Authorized='True']")
                {
                    configModFound = modsCollection[i];
                }
            }

            // Remove it and save the change to the configuration database
            modsCollection.Remove(configModFound);
            myService.Update();

            // Reapply all the configuration modifications
            myService.Farm.Services.GetValue<SPWebService>().ApplyWebConfigModifications();
        }


        // Uncomment the method below to handle the event raised after a feature has been installed.

        public override void FeatureInstalled(SPFeatureReceiverProperties properties)
        {
            
        }


        // Uncomment the method below to handle the event raised before a feature is uninstalled.

        public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
        {
            
        }

        // Uncomment the method below to handle the event raised when a feature is upgrading.

        //public override void FeatureUpgrading(SPFeatureReceiverProperties properties, string upgradeActionName, System.Collections.Generic.IDictionary<string, string> parameters)
        //{
        //}
    }
}
